<?php

class ImagesModule extends Module {

    protected $models = array('Image', 'ImageCategory');
    protected $helpers = array('File');

    public function __construct() {
        parent::__construct(dirname(__FILE__));
    }

    public function index() {

        $offset = isset($_GET['offset']) ? $_GET['offset'] : 0;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 20;

        $images = $this->Image->find(array(
            'fields' => array('id', 'ImageName', 'ThumbPath', 'ImageFile'),
            'order' => array('id' => 'desc'),
            'limit' => $limit,
            'offset' => $offset)
        );

        $this->set('images', $images);
    }

    public function add() {
        $this->autoLayout = false;

        if(isset($_POST['Image'])) {
            $this->autoRender = false;

            $data = $this->File->uploadFile('../assets/images/' . SITE_TAG . '/content/pics/', "img" . date('Ymd_his'));
            $image_data = array();

            foreach($data['files'] as $file) {
                $thumbPath = $this->File->generateThumb($file['file_name'], '../assets/images/' . SITE_TAG . '/content/thumbs/', 180);

                if(isset($_POST['max-width'])) {
                    $imagePath = $this->File->generateThumb($file['file_name'], '../assets/images/' . SITE_TAG . '/content/pics/', $_POST['max-width']);
                }

                $image_name = $_POST['Image']['ImageName'] != "" ? $_POST['Image']['ImageName'] : basename($file['file_name']);

                $image = new Image(array(
                    'ImageName' => $image_name,
                    'ImageDescription' => $_POST['Image']['ImageDescription'] ? $_POST['Image']['ImageDescription'] : $image_name,
                    'ImageFile' => $file['file_name'],
                    'ThumbPath' => $thumbPath,
                    'MimeType' => $file['mime_type'],
                    'UseSection' => $_POST['Image']['UseSection'],
                    'SiteID' => SITE_ID)
                );

                $image->save();

                $image_data[] = $image->getDataArray();
            }

            echo json_encode($image_data);

            return;
        }

        $categories = $this->ImageCategory->find(array('SiteID' => SITE_ID));
        $this->set('categories', $categories);
    }

    public function delete() {
        $this->autoRender = false;
        $this->autoLayout = false;

        if(isset($_GET['id'])) {
            $image = new Image($_GET['id']);
            $image->delete();

            echo json_encode(array('id' => $_GET['id']));
        }
    }

    public function delete_selected() {
        $this->autoRender = false;

        if(isset($_POST['ids'])) {
            $ids = explode(",", $_POST['ids']);
            $this->Image->deleteAll(array('id IN' => $ids));
        }
    }

    public function update_image_name() {
        $this->autoRender = false;

        if(isset($_POST['image_name'])) {
            $image = new Image($_POST['id']);
            $image->set('ImageName', $_POST['image_name']);
            $image->save();
        }
    }

}

?>